package tables

import (
	"errors"
	"fmt"
	"mime/multipart"
	"peilian-api/app/global/variable"

	"gorm.io/gorm"
)

const (
	QCommonId        = 1 // 通用岗位题库 默认是 1 数据库中id必须是1
	QCivilServant    = 2 // 公务员题库id=2  数据库中id必须是2
	IsAvailable      = 0 // 可用
	NoAvailable      = 1 // 不可用，敬请期待
	QPMId            = 4
	BankTrainee      = 12
	SelfIntroduction = 14
	Experiencing     = 15
	Star             = 16
	Complement       = 17
)

type Career struct { //职业表，也叫题库表
	Model
	Name              string                `json:"name,omitempty" gorm:"type:varchar(50);not null;unique;comment:题库名称" form:"name"`
	CareerPhoto       string                `json:"career_photo" gorm:"type:varchar(255);comment:图库展示图片"`
	PhotoIsDefault    *bool                 `json:"photo_is_default" gorm:"default:false;comment:自定义岗位的背景图片是否是默认的"`
	CareerPhotoStream *multipart.FileHeader `json:"-" gorm:"-" form:"career_photo"`
	CareerPhotoBeida  string                `json:"career_photo_beida" gorm:"type:varchar(255);comment:图库展示图片(北大红)"`
	CareerMsg         *string               `json:"career_msg" gorm:"type:varchar(255);comment:信息描述" form:"career_msg"`
	IsAvailable       uint                  `json:"is_available" gorm:"default:0;comment:是否可用 0可用 1不可用 2内部使用"`
	OrderId           uint                  `json:"order_id" gorm:"default:0;comment:展示顺序"`
	SelectRule        string                `json:"select_rule,omitempty" gorm:"default:\"\";comment:抽题规则,除通用岗位外,其他数组至少有三个元素"`
	Type              string                `json:"type" gorm:"type:varchar(50);comment:职业类别,互联网类、传统类"`
	SpecialBase       string                `json:"special_base,omitempty" gorm:"type:longtext;comment:专项基础练习,能力展示列表"`
	SpecialAdvance    string                `json:"special_advance,omitempty" gorm:"type:longtext;comment:专项进阶练习,能力展示列表"`
	Label             string                `json:"label" gorm:"type:varchar(50);comment:分类标签,推荐" form:"label"`
	Questions         []Question            `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`
	ReportType        string                `gorm:"varchar(20);default:pdf;comment:报告的类型" json:"report_type"`
	MongoRangeId      string                `gorm:"type:varchar(50);comment:白泽中的c_range_id" json:"mongo_range_id"`
	IsRelease         *bool                 `gorm:"default:true;comment:职业是否发布" json:"is_release" form:"is_release"`
	QuestionNum       int                   `gorm:"type:int(10);comment:该职位的题目数量(只有自定义职位)" json:"question_num" form:"question_num"`
	BdShow            *bool                 `gorm:"default:true;comment:北大职位是否展示:0不展示 1展示" json:"bd_show" form:"bd_show"`

	AgentId      uint       `gorm:"default:0;comment:自定义岗位有哪个客户创建(仅限自定义岗位)" json:"agent_id,omitempty"`
	ProfessionId *uint      `gorm:"index;comment:岗位所在的专业信息" json:"profession_id" form:"profession_id"`
	Profession   Profession `json:"profession"`

	Sessions     []Session   `json:"-"`
	CusQuestions []*Question `gorm:"many2many:custom_career" json:"-"`

	NotShowAgentIds string `gorm:"type:varchar(255);default:\"\";comment:默认的职业 有的学校不需要展示，记录所有不显示的学校信息" json:"-"`
	AdminCareerShow *bool  `gorm:"-" json:"admin_career_show" form:"admin_career_show"`

	CareerType string `json:"career_type" gorm:"default:interview;type:varchar(50);comment:interview/discussion"` //职业大类型，无领导讨论或面试
}

type CareerUpdate struct {
	*Career
	CareerMsg *string `json:"career_msg" form:"career_msg"`
}

func (c *Career) TableName() string {
	return "career"
}

func (c *Career) AfterDelete(tx *gorm.DB) (err error) {
	if err := variable.DB.MySQL.Delete(&CustomCareer{}, "career_id=?", c.ID).Error; err != nil {
		return err
	}
	return nil
}

func (c *Career) SetCustomDefault() {
	c.IsAvailable = 0
	var maxOrderId uint
	if err := variable.DB.MySQL.Model(c).Select("max(order_id)").Where("order_id >= 10000 and order_id < 20000").
		Scan(&maxOrderId).Error; err != nil {
		fmt.Println(err.Error())
		c.OrderId = 10000
	} else {
		c.OrderId = maxOrderId + 1
	}
	c.Type = "自定义"
	c.ReportType = "custom"
	f := false
	c.IsRelease = &f
	c.CareerPhotoBeida = c.CareerPhoto
}

func (c *Career) SaveValidate() error {
	if c.QuestionNum > 0 && (c.QuestionNum < 3 || c.QuestionNum > 8) {
		return errors.New("题目数量为3-8道")
	}
	if len(c.Label) == 0 {
		return errors.New("职业类型不能为空")
	}
	if len(c.Name) == 0 {
		return errors.New("职业名称不能为空")
	}
	if len(c.Name) > 8*3 {
		return errors.New("职业名称不能超过8个字")
	}
	if c.CareerMsg != nil && len(*c.CareerMsg) > 20*3 {
		return errors.New("职业介绍不能超过20个字")
	}
	return nil
}

func (c *Career) SaveDiscussionValidate() error {
	if c.QuestionNum != 1 {
		return errors.New("题目数量应为1道")
	}
	if len(c.Label) == 0 {
		return errors.New("职业类型不能为空")
	}
	if len(c.Name) == 0 {
		return errors.New("职业名称不能为空")
	}
	if len([]rune(c.Name)) > 30 {
		return errors.New("职业名称不能超过30个字")
	}
	if c.CareerMsg != nil && len([]rune(*c.CareerMsg)) > 1000 {
		return errors.New("职业介绍不能超过1000个字")
	}
	return nil
}

func (c *Career) UpdateValidate() error {

	if c.QuestionNum > 0 && (c.QuestionNum < 3 || c.QuestionNum > 8) {
		return errors.New("题目数量为3-8道")
	}
	if len(c.Name) > 8*3 {
		return errors.New("职业名称不能超过8个字")
	}
	if c.CareerMsg != nil && len(*c.CareerMsg) > 20*3 {
		return errors.New("职业介绍不能超过20个字")
	}
	return nil
}

func (c *Career) UpdateDiscussionValidate() error {

	if c.QuestionNum > 0 && c.QuestionNum != 1 {
		return errors.New("题目数量为3-8道")
	}
	if len([]rune(c.Name)) > 30 {
		return errors.New("职业名称不能超过30个字")
	}
	if c.CareerMsg != nil && len([]rune(*c.CareerMsg)) > 1000 {
		return errors.New("职业介绍不能超过1000个字")
	}
	return nil
}

func GetCareerInfoById(id uint) (career Career, err error) {
	d := variable.DB.MySQL.Where("id = ?", id).Find(&career)
	return career, d.Error
}

func (c *Career) GetCareerInfoListByWhereStr(whereStr string) ([]Career, error) {
	var careerList []Career
	d := variable.DB.MySQL.Model(&c).Where(whereStr).Find(&careerList)
	return careerList, d.Error
}

//type CareerType struct {
//	Model
//	Label   string `json:"label" gorm:"type:varchar(50);unique;comment:分类标签,推荐"`
//	AgentId uint   `gorm:"default:0;comment:自定义岗位有哪个客户创建(仅限自定义岗位)" json:"agent_id"`
//}
//
//func (ct *CareerType) TableName() string {
//	return "career_type"
//}
